﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class TodayRobotAlarmsDistributionController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(TodayRobotAlarmDistributionDtoOutput))]
        public IHttpActionResult Get(int id)
        {
            #region Test
            if (Helper.IsTest)
            {
                TodayRobotAlarmDistributionDtoOutput testData = new TodayRobotAlarmDistributionDtoOutput();
                testData.Date = System.DateTime.Now.Date;
                testData.RobotID = id;
                testData.SlicesInfo = new List<AlarmSliceDto>();
                AlarmSliceDto info1 = new AlarmSliceDto() { AlarmCode = "ACode1", Ratio = 10.5M, Quantity = 105M };
                AlarmSliceDto info2 = new AlarmSliceDto() { AlarmCode = "ACode2", Ratio = 20M, Quantity = 200M };
                AlarmSliceDto info3 = new AlarmSliceDto() { AlarmCode = "ACode3", Ratio = 30M, Quantity = 300M };
                AlarmSliceDto info4 = new AlarmSliceDto() { AlarmCode = "ACode4", Ratio = 39.5M, Quantity = 395M };
                testData.SlicesInfo.Add(info1);
                testData.SlicesInfo.Add(info2);
                testData.SlicesInfo.Add(info3);
                testData.SlicesInfo.Add(info4);
                return Ok(testData);
            }
            #endregion

            DateTime today = System.DateTime.Now.Date;
            var alarmsSummary = (from w in db.Alarm_History.Where(a => a.Time >= today && a.Robot_ID == id)
                                 group w by new { w.Alarm_Severity } into grp
                                 select new
                                 {
                                     grp.Key.Alarm_Severity,
                                     TotalQuantity = grp.Count()
                                 }).OrderByDescending(a => a.TotalQuantity);
            if (alarmsSummary.Count() <= 0)
            {
                TodayAllRobotsAlarmDistributionDtoOutput noData = new TodayAllRobotsAlarmDistributionDtoOutput();
                noData.Date = System.DateTime.Now.Date;

                noData.SlicesInfo = new List<AlarmSliceDto>();
                AlarmSliceDto info1 = new AlarmSliceDto() { AlarmCode = "", Ratio = 0M, Quantity = 0M };
                noData.SlicesInfo.Add(info1);
                return Ok(noData);
            }
            TodayAllRobotsAlarmDistributionDtoOutput Data = new TodayAllRobotsAlarmDistributionDtoOutput();
            Data.Date = System.DateTime.Now.Date;
            Data.SlicesInfo = new List<AlarmSliceDto>();
            int totalQuantity = alarmsSummary.Sum(a => a.TotalQuantity);
            foreach (var item in alarmsSummary)
            {
                AlarmSliceDto slice = new AlarmSliceDto() {AlarmSeverity = item.Alarm_Severity, Ratio = Decimal.Round(Convert.ToDecimal(item.TotalQuantity) / totalQuantity, 4), Quantity = item.TotalQuantity };
                Data.SlicesInfo.Add(slice);
            }
            return Ok(Data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
